<?php
include("include/config.inc.php");

$pageName = 'logsheetList';
if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $msg = "Record Not Found";
  
  if(isset($_POST['notOkButton']))
  {
	  $selPlanning = "SELECT incomingId, partyId, date, quantity, weight, processId1, processId2, processId3, noOfProcess,
	                         temp1, soaking1, fId1, temp2, soaking2, fId2, temp3, soaking3, fId3, logSheet1, logSheet2, logSheet3,
	                         okNotOk1, okNotOk2, okNotOk3, status, parentPlanningId
	                    FROM planning
	                   WHERE planningId =".$_POST['planningId'];
    $selPlanningRes = mysql_query($selPlanning);
    if($planRow = mysql_fetch_array($selPlanningRes))
    {
    	$process1Reorder = isset($_POST['process1Reorder']) && $_POST['process1Reorder'] > 0 ? 0 : 1;
    	$process2Reorder = isset($_POST['process2Reorder']) && $_POST['process2Reorder'] > 0 ? 0 : 1;
    	$process3Reorder = isset($_POST['process3Reorder']) && $_POST['process3Reorder'] > 0 ? 0 : 1;
    	
    	if($planRow['processId1'] == 0)
    	  $process1Reorder = -1;
    	if($planRow['processId2'] == 0)
    	  $process2Reorder = -1;
    	if($planRow['processId3'] == 0)
    	  $process3Reorder = -1;
    	
    	if($process1Reorder != 0)
    	{
    		$temp1    = $planRow['temp1'];
    		$soaking1 = $planRow['soaking1'];
    		$furnace1 = $planRow['fId1'];
    		$logSheet1 = $planRow['logSheet1'];
    	}
      else
      {
    		$temp1    = 0;
    		$soaking1 = 0;
    		$furnace1 = 0;
    		$logSheet1 = 0;
      }    	
    	if($process2Reorder != 0)
    	{
    		$temp2    = $planRow['temp2'];
    		$soaking2 = $planRow['soaking2'];
    		$furnace2 = $planRow['fId2'];
    		$logSheet2 = $planRow['logSheet2'];
    	}
      else
      {
    		$temp2    = 0;
    		$soaking2 = 0;
    		$furnace2 = 0;
    		$logSheet2 = 0;
      }    	
      
    	if($process3Reorder != 0)
    	{
    		$temp3    = $planRow['temp3'];
    		$soaking3 = $planRow['soaking3'];
    		$furnace3 = $planRow['fId3'];
    		$logSheet3 = $planRow['logSheet3'];
    	}
      else
      {
    		$temp3    = 0;
    		$soaking3 = 0;
    		$furnace3 = 0;
    		$logSheet3 = 0;
      }    	

    	if($planRow['date'] == NULL)
    	{
    		$planRow['date'] = date('Y-m-d');
    	}
    	$insNewRecord = "INSERT INTO planning (incomingId, partyId, date, quantity, weight, processId1, processId2, processId3, noOfProcess, 
    	                                       temp1, soaking1, fId1, temp2, soaking2, fId2, temp3, soaking3, fId3, logSheet1, 
    	                                       logSheet2, logSheet3, okNotOk1, okNotOk2, okNotOk3,status, parentPlanningId)
         	                   VALUES (".$planRow['incomingId'].",".$planRow['partyId'].",'".$planRow['date']."',".$planRow['quantity'].",
         	                          ".$planRow['weight'].",".$planRow['processId1'].",".$planRow['processId2'].",".$planRow['processId3'].",
         	                          ".$planRow['noOfProcess'].",'".$temp1."','".$soaking1."','".$furnace1."','".$temp2."','".$soaking2."',
         	                          '".$furnace2."','".$temp3."','".$soaking3."','".$furnace3."',".$logSheet1.",".$logSheet2.",".$logSheet3.",
         	                          ".$process1Reorder.",".$process2Reorder.", ".$process3Reorder.",'inplanning', ".$_POST['planningId'].")";
      $insNewRecordRes = mysql_query($insNewRecord);
      $insertedId = mysql_insert_id();
      
      $updateLogsheet = "UPDATE logsheet
                            SET status = 0
                          WHERE planningId = ".$_POST['planningId'];
      //$updateLogsheetRes = mysql_query($updateLogsheet);
    	$processNo = $_POST['processNo'];
    	$loopRecord = 0;
    	
    	if(isset($_POST['process3Reorder']))
    	{
    		$loopRecord = $loopRecord + 1;
    	}
    	
    	if(isset($_POST['process2Reorder']))
    	{
    		$loopRecord = $loopRecord + 1;
    	}
    	if(isset($_POST['process1Reorder']))
    	{
    		$loopRecord = $loopRecord + 1;
    	}
    	
    	for($i = 0; $i < $loopRecord; $i++)
    	{
    		$updateSheet = "UPDATE logsheet
    		                   SET status = 0, notOkReason = '".$_POST['notOkReason']."'
    		                 WHERE planningId = ".$_POST['planningId']."
      	                   AND processNo = ".$processNo;
        $updateSheetRes = mysql_query($updateSheet);
        $processNo = $processNo - 1;
    	}
    }
    if($insNewRecordRes)
    {
    	$updatePlanRecord = "UPDATE planning 
    	                        SET status = 'not ok'
    	                      WHERE planningId = ".$_POST['planningId'];
      $updatePlanRecordRes = mysql_query($updatePlanRecord);
    }
	  //Effecting of Planning Record : Ends
  }
  
  $logsArray = array();
	$i =0;
	$selectLogData = "SELECT ";
  $selectLogRecords = "SELECT logSheetId,logsheet.incomingId,processNo,logsheet.planningId,logsheetNo,
                              DATE_FORMAT(logsheetDate,'%d-%m-%Y')AS logsheetDate,logsheet.status,processName,
                              quantity,planning.weight,furnace1.furnaceName AS fName1,
                              furnace2.furnaceName AS fName2,furnace3.furnaceName AS fName3,materialName,hardne,
                              incoming.challan,partyName
												 FROM logsheet
												 LEFT JOIN process ON logsheet.processId = process.processId
												 JOIN planning ON logsheet.planningId = planning.planningId
												 JOIN incoming ON logsheet.incomingId = incoming.incomingId
												 JOIN party ON incoming.partyId = party.partyId
												 JOIN material ON incoming.materialId = material.materialId
                         LEFT JOIN furnace AS furnace1 ON planning.fId1 = furnace1.fId
                         LEFT JOIN furnace AS furnace2 ON planning.fId2 = furnace2.fId
                         LEFT JOIN furnace AS furnace3 ON planning.fId3 = furnace3.fId
												WHERE logsheet.status = '-1' OR logsheet.status = '1'
												GROUP BY challan
												ORDER BY logSheetId";                                     
															 
	$selectLogRecordsRes = mysql_query($selectLogRecords);
	while($logRow = mysql_fetch_array($selectLogRecordsRes))
	{
	  $logsArray[$i]['logSheetId']   = $logRow['logSheetId'];
	  $logsArray[$i]['incomingId']   = $logRow['incomingId'];
	  $logsArray[$i]['processNo']    = $logRow['processNo'];
	  $logsArray[$i]['processName']  = $logRow['processName'];
	  $logsArray[$i]['planningId']   = $logRow['planningId'];
	  $logsArray[$i]['logsheetNo']   = $logRow['logsheetNo'];
	  $logsArray[$i]['logsheetDate'] = $logRow['logsheetDate'];
	  $logsArray[$i]['quantity']     = $logRow['quantity'];
	  $logsArray[$i]['weight']       = $logRow['weight'];
	  $logsArray[$i]['furnace1']     = $logRow['fName1'];
	  $logsArray[$i]['furnace2']     = $logRow['fName2'];
	  $logsArray[$i]['furnace3']     = $logRow['fName3'];
	  $logsArray[$i]['materialName'] = $logRow['materialName'];
	  $logsArray[$i]['hardne']       = $logRow['hardne'];
	  $logsArray[$i]['challan']      = $logRow['challan'];
	  $logsArray[$i]['partyName']    = $logRow['partyName'];
	                                 
	  $logsArray[$i]['status'] 		   = $logRow['status'];
	  $i++;
	}
	include("./bottom.php");
	$smarty->assign("logsArray",$logsArray);
	//$smarty->assign("plannArray",$plannArray);
	$smarty->assign("msg",'<font size="6">No Record Found</font>');
	$smarty->display("logsheetList.tpl");
}
?>
